package com.aliyuncs.aui.service.impl;

import com.aliyuncs.aui.common.utils.MySQLUtil;
import com.aliyuncs.aui.dto.res.TableInfo;
import com.aliyuncs.aui.dto.res.CommonResponse;
import com.aliyuncs.aui.service.TablesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TablesServiceImpl implements TablesService {

    private final MySQLUtil mySQLUtil;

    public TablesServiceImpl(MySQLUtil mySQLUtil) {
        this.mySQLUtil = mySQLUtil;
    }

    @Override
    public List<TableInfo> getAvailableTables(String tenantId, Integer capacity, String tableType, String tablePosition) {
        String sql = "SELECT * FROM tables WHERE tenant_id = ? AND status = '空闲'";
        List<Object> params = new ArrayList<>();
        params.add(tenantId);

        if (capacity != null) {
            sql += " AND capacity >= ?";
            params.add(capacity);
        }
        if (tableType != null && !tableType.isEmpty()) {
            sql += " AND table_type = ?";
            params.add(tableType);
        }
        if (tablePosition != null && !tablePosition.isEmpty()) {
            sql += " AND table_position = ?";
            params.add(tablePosition);
        }

        List<TableInfo> tables = new ArrayList<>();
        try (ResultSet rs = mySQLUtil.executeQuery(sql, params.toArray())) {
            while (rs.next()) {
                tables.add(mapResultSetToTableInfo(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return tables;
    }

    @Override
    public CommonResponse updateTableState(String tenantId, String tableNumber, String state) {
        if (!"占用".equals(state) && !"空闲".equals(state)) {
            return CommonResponse.builder()
                .code(400)
                .message("状态值只能是'占用'或'空闲'")
                .build();
        }

        String sql = "UPDATE tables SET status = ? WHERE tenant_id = ? AND table_number = ?";
        try {
            int rows = mySQLUtil.executeUpdate(sql, state, tenantId, tableNumber);
            if (rows > 0) {
                return CommonResponse.builder()
                    .code(200)
                    .message("状态更新成功")
                    .build();
            }
            return CommonResponse.builder()
                .code(404)
                .message("未找到匹配的餐桌记录")
                .build();
        } catch (SQLException e) {
            e.printStackTrace();
            return CommonResponse.builder()
                .code(500)
                .message("数据库操作失败：" + e.getMessage())
                .build();
        }
    }

    private TableInfo mapResultSetToTableInfo(ResultSet rs) throws SQLException {
        TableInfo info = new TableInfo();
        info.setTableNumber(rs.getString("table_number"));
        info.setStatus(rs.getString("status"));
        info.setCapacity(rs.getInt("capacity"));
        info.setTableType(rs.getString("table_type"));
        info.setTablePosition(rs.getString("table_position"));
        info.setPicUrl(rs.getString("picUrl"));
        return info;
    }
}
